using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Drawing;

namespace PageDraw
{
    class XMLHandler
    {
        private XmlElement rootElement = null;
        private XmlDocument document = null;
        public XMLHandler() 
        {
            document = new XmlDocument();
        }

        public XMLHandler(string xmlFilePath)
            : this()
        {
            loadXmlFile(xmlFilePath);
        }

        private void loadXmlFile(string xmlFilePath)
        {
            try
            {
                document.Load(xmlFilePath);
                rootElement = document.DocumentElement;
            }
            catch (Exception ee)
            {
                throw new Exception("Xml File [" + xmlFilePath + "] load failure.", ee);
            }
        }

        public string getXmlNodeInnerText(string xpath)
        {   
            XmlNode xmlNode = rootElement.SelectSingleNode(xpath);
            string txt = xmlNode.InnerText;
            return txt;
        }
      
        public string getXmlNodeValue(string xpath)
        {
            XmlNode xmlNode = rootElement.SelectSingleNode(xpath);
            string txt = xmlNode.Value;
            return txt;
        }

        public void ProduceXml(string xmlFilePath, GraphicsTree tree)
        {
            try
            {
                XmlNode xnode = document.CreateXmlDeclaration("1.0", "UTF-8", null);
                document.AppendChild(xnode);

                rootElement = document.CreateElement("DrawObjects");
                document.AppendChild(rootElement);

                XmlElement me = MainDataPanelXml(tree, rootElement);
                XmlElement mse = MySelectPanelXml(tree, me);
                List<XmlElement> tbeList = TableBasePanelXml(tree, me);
                List<XmlElement> deList = DataPanelXml(tree, me);

                document.Save(xmlFilePath);
                
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        private List<XmlElement> DataPanelXml(GraphicsTree tree, XmlElement me)
        {
            if (me != null)
            {
                List<XmlElement> deList = new List<XmlElement>();
                foreach (TreeNode node in tree.DrawObjects)
                {
                    DrawObject obj = node.Tag as DrawObject;
                    if (obj != null)
                    {
                        if (obj is DrawDataPanel)
                        {
                            XmlElement elem = obj.GetXmlElement(document);
                            Rectangle rect = obj.GetRect();
                            foreach (TreeNode nd in tree.DrawObjects)
                            {
                                DrawObject o = nd.Tag as DrawObject;
                                if (o != null && o != obj && rect.Contains(o.GetRect()))
                                {
                                    XmlElement em = o.GetXmlElement(document);
                                    elem.AppendChild(em);
                                }
                            }
                            me.AppendChild(elem);
                            deList.Add(elem);
                        }
                    }
                }
                return deList;
            }
            return null;
        }

        private List<XmlElement> TableBasePanelXml(GraphicsTree tree, XmlElement me)
        {
            List<XmlElement> list = new List<XmlElement>();
            if (me != null)
            {
                foreach (TreeNode node in tree.DrawObjects)
                {
                    DrawObject obj = node.Tag as DrawObject;
                    if (obj != null)
                    {
                        if (obj is DrawTableBasePanel)
                        {
                            XmlElement elem = obj.GetXmlElement(document);
                            Rectangle rect = obj.GetRect();
                            foreach (TreeNode nd in tree.DrawObjects)
                            {
                                DrawObject o = nd.Tag as DrawObject;
                                if (o != null && o != obj && rect.Contains(o.GetRect()))
                                {
                                    XmlElement em = o.GetXmlElement(document);
                                    elem.AppendChild(em);
                                }
                            }
                            me.AppendChild(elem);
                            list.Add(elem);
                        }
                    }
                }
            }
            return list;
        }

        private XmlElement MySelectPanelXml(GraphicsTree tree, XmlElement me)
        {
            if (me != null)
            {
                foreach (TreeNode node in tree.DrawObjects)
                {
                    DrawObject obj = node.Tag as DrawObject;
                    if (obj != null)
                    {
                        if (obj is DrawMySelectPanel)
                        {
                            XmlElement elem = obj.GetXmlElement(document);
                            Rectangle rect = obj.GetRect();
                            foreach (TreeNode nd in tree.DrawObjects)
                            {
                                DrawObject o = nd.Tag as DrawObject;
                                if (o != null && o != obj && rect.Contains(o.GetRect()))
                                {
                                    XmlElement em = o.GetXmlElement(document);
                                    elem.AppendChild(em);
                                }
                            }
                            me.AppendChild(elem);
                            return elem;
                        }
                    }
                }
            }
            return null;
        }

        private XmlElement MainDataPanelXml(GraphicsTree tree, XmlElement rootElem)
        {
            foreach (TreeNode node in tree.DrawObjects)
            {
                DrawObject obj = node.Tag as DrawObject;
                if (obj != null)
                {
                    if (obj is DrawMainDataPanel)
                    {
                        XmlElement elem = obj.GetXmlElement(document);
                        rootElem.AppendChild(elem);
                        return elem;
                    }
                }
            }
            return null;
        }
    }
}
